/***************************************************************************
*   Copyright (C) 2011 by Chuck Gao & Alex Liu                             *
*                                                                          *
*   This program is free software: you can redistribute it and/or modify   *
*   it under the terms of the GNU General Public License as published by   *
*   the Free Software Foundation, either version 3 of the License, or      *
*   (at your option) any later version.                                    *
*                                                                          *
*   This program is distributed in the hope that it will be useful,        *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
*   GNU General Public License for more details.                           *
*                                                                          *
*   You should have received a copy of the GNU General Public License      *
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
*                                                                          *
*   Contact author: chuckgao.cg@gmail.com                                  *
*                   alex2202375@gmail.com                                  *
***************************************************************************/

import QtQuick 1.0
import Qt3D 1.0
import Qt3D.Shapes 1.0
import Qt.labs.gestures 1.0

Item {
    id: countrySelectView
    anchors.fill: parent
    smooth: true

    property int exitAnimationDuration: 400 //ms

    ParallelAnimation {
        id: exitAnimation
        running: false
        NumberAnimation { target: earth; property: "scale"; to: 0.1; duration: countrySelectView.exitAnimationDuration }
        NumberAnimation { target: earth; property: "opacity"; to: 0; duration: countrySelectView.exitAnimationDuration }
        NumberAnimation { target: countrySelector; property: "x"; to: -countrySelector.width; duration: countrySelectView.exitAnimationDuration }
        NumberAnimation { target: recommendedTag; property: "x"; to: cusineView.width; duration: countrySelectView.exitAnimationDuration }
    }

    Viewport {
        id: earth
        width: 360; height: 360
        anchors.horizontalCenter: parent.horizontalCenter
        smooth: true

        light: Light {
            direction: Qt.vector3d(1, 0, 2)
        }

        Sphere {
            id: s
            scale: 1.5
            levelOfDetail: 10 // The max detail level. Maybe limitted in mobile phone
            axis: Qt.YAxis

            effect: Effect {
                texture: "../../../Images/earth.jpg"
            }

            transform: [
                Rotation3D {
                    id: earthRotation
                    axis: Qt.vector3d(0, 1, 0)
                }
            ]

            NumberAnimation {
                id: earthRotationAnimation
                running: false
                target: earthRotation;
                property: "angle";
                from: earthOldAngle;
                to: earthCurrentAngle;
                duration: 1000

                onCompleted: {
                    earthOldAngle = earthCurrentAngle
                }
            }
        }
    }

    Item {
        id: countrySelector
        width: 320; height: 160
        x: (cusineView.width - countrySelector.width)/2
        anchors.top: earth.bottom
        // Do not set anchors.horizontalCenter, we need some animation on x change

        property int currentIndex: 0

        ListModel {
            id: countryModel
            ListElement {
                country: "China"
            }
            ListElement {
                country: "France"
            }
            ListElement {
                country: "United Kingdom"
            }
            ListElement {
                country: "United States"
            }
        }

        Text {
            width: 240
            height: 160
            anchors.centerIn: parent
            text: countryModel.get(countrySelector.currentIndex).country
            font.family: "Freestyle Script"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pointSize: 60
            color: "white"
        }

        //        GestureArea {
        //            anchors.fill: parent
        //            onPan: {
        //                console.debug("GestureArea onPan:", gesture.acceleration)
        //                countryModel.index = countryModel.index - 1;
        //            }
        //        }

        /*******************************************
         * Test code -- Enter
         *****************************************/
        MouseArea {
            anchors.fill: parent
            onClicked: {
                var index = countrySelector.currentIndex;
                index++;

                if(index >= countryModel.count)
                {
                    index = 0;
                }

                countrySelector.currentIndex = index;
                earthCurrentAngle = earthOldAngle + 30;
                earthRotationAnimation.running = true
            }

            onPressAndHold: {
                // Enter the subview. Depends on the data structure
                exitAnimation.running = true
                cusineViewLoader2.source = "CuisineSelectView.qml"
                cusineViewLoader1.source = ""
            }
        }
        /*******************************************
         * Test code -- Exit
         *****************************************/
    }

    ListModel {
        id: recommendedModel
        ListElement {
            recommended: "Kung Pao Chicken"
        }
        ListElement {
            recommended: "Tea smoked duck"
        }
        ListElement {
            recommended: "123"
        }
    }

    ListView {
        id: recommendedTag
        width: 360
        height: 120
        // Do not set anchors.horizontalCenter, we need some animation on x change
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        model: recommendedModel
        delegate:
            Text {
            x: 100 + 80 * Math.random()
            width: 160
            height: 40
            text: recommended
            font.family: "Freestyle Script"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pointSize: 24
            color: "lightblue"
        }
    }
}
